package com.hotel.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.CounterLogic;
import com.hotel.BL.POSLogic;

import data.Counter;
import data.POS;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class CounterService extends HttpServlet{
	//http://localhost:8080/counterService?func=addCounter&title=1&description=aa&type=ddd
	//http://localhost:8080/counterService?func=editCounter&&counterId=869&title=1&description=aa&type=dddscxzz
	//http://localhost:8080/counterService?func=delCounter&counterId=869
	//http://localhost:8080/counterService?func=changeCounter&counterId=869
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addCounter")){
				AddCounter(req,resp);
			}else if(doClass.equals("addPOSCounter")){
				AddPOSCounter(req,resp);
			}else if(doClass.equals("editCounter")){
				EditCounter(req,resp);
			}else if(doClass.equals("delCounter")){
				DeleteCounter(req,resp);
			}else if(doClass.equals("deletePOSCounter")){
				DeletePOSCounter(req,resp);
			}else if(doClass.equals("changeCounter")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getAllCounter")){
				GetAllCounter(req,resp);
			}else if(doClass.equals("getAllCounter4Admin")){
				GetAllCounter4Admin(req,resp);
			}else if(doClass.equals("getAllPOSCounter4Admin")){
				GetAllPOSCounter4Admin(req,resp);
			}else if(doClass.equals("getCounterById")){
				GetCounterById(req,resp);
			}else if(doClass.equals("closeCounter")){
				CloseCounter(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddCounter(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		new CounterLogic().AddCounter(hotelId,title);
		GetAllCounter4Admin(req, resp);
	}

	private void AddPOSCounter(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		long posId = Long.parseLong(req.getParameter("posId"));
		Counter c = new CounterLogic().AddCounter(hotelId,title);
		new POSLogic(req,resp).EditPOSCounter(posId, c.getId());
		GetAllCounter4Admin(req, resp);
	}
	
	private void EditCounter(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long counterId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		new CounterLogic().EditCounterTitle(counterId,title);
		GetAllCounter4Admin(req, resp);
	}

	private void DeleteCounter(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String counterId = req.getParameter("id");	
		for(int i = 0; i < counterId.split(",").length ; i++){
			new CounterLogic().DeleteCounter(Long.parseLong(counterId.split(",")[i]));
		}
		GetAllCounter4Admin(req, resp);
	}
	
	private void DeletePOSCounter(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String counterId = req.getParameter("counterId");
		long posId = Long.parseLong(req.getParameter("posId"));
		String[] arrayCounterId = counterId.split(",");
		POSLogic posLogic = new POSLogic(req,resp);
		for(int i = 0; i < arrayCounterId.length ; i++){
			posLogic.DeletePOSCounter(posId,Long.parseLong(arrayCounterId[i]));
		}
		GetAllCounter4Admin(req, resp);
	}
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long counterId = Long.parseLong(req.getParameter("counterId"));

		Object obj = new CounterLogic().ChangeStatus(counterId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllCounter(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<Counter> obj = new CounterLogic().GetAllCountert(hotelId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllPOSCounter4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		//HttpSession session=req.getSession(true);	
		//long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		long posId = Long.parseLong(req.getParameter("posId"));
		POS objpos = new POSLogic(req,resp).GetPOS(posId);
		List<HashMap<String,Object>> objHashMapCounter = new ArrayList<HashMap<String,Object>>();
		String arrPOSCounter[] = objpos.getCounterString().split(",");
		int dataAmount = arrPOSCounter.length;
		if(!arrPOSCounter[0].equals("")){
			for(int i =0;i<arrPOSCounter.length;i++){
				if(objHashMapCounter.size()>=line){
					continue;
				}
				if(i>=offset){
					HashMap<String,Object> objHashMapCounterset = new HashMap<String,Object>();
					Counter objCounter = new CounterLogic().GetCounter(Long.parseLong(arrPOSCounter[i]));
					objHashMapCounterset.put("id", objCounter.getId());
					objHashMapCounterset.put("hotelId", objCounter.getHotelId());
					objHashMapCounterset.put("lastUserString", objCounter.getLastUserString());
					objHashMapCounterset.put("title", objCounter.getTitle());
					objHashMapCounterset.put("status", objCounter.isStatus());
					objHashMapCounterset.put("avaliable", objCounter.isAvaliable());
					objHashMapCounter.add(objHashMapCounterset);
				}
			}
		}else
			dataAmount = 0;
		Gson gson = new Gson();
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataAmount);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(Double.parseDouble(String.valueOf(arrPOSCounter.length))/line));
		data.put("data", objHashMapCounter);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetAllCounter4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		List<Counter> obj = new CounterLogic().GetAllCounter4AdminLimit(hotelId,offset,line);
		double dataSize = new CounterLogic().GetAllCounter4Admin(hotelId).size();
		Gson gson = new Gson();
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetCounterById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		Counter c = new CounterLogic().GetCounter(id);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(c));
	}
	
	private void CloseCounter(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		long id = Long.parseLong(req.getParameter("id"));
		boolean c = new CounterLogic().CloseCounter(id);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(c));
	}
}
